﻿#!/usr/bin/env python
# -*- coding:utf-8 -*-

' error usage '

__author__='zhaoxp'

def error_example(a):
	try:
		print "try..."
		r=10/int(a)
		print 'result =',r
	except ValueError,e:
		print 'ValueError',e
	except ZeroDivisionError,e:
		print 'ZeroDivisionError',e
	else:
		print 'no error!'
	finally:
		print 'finally'
	print 'end'

class DivisionError(StandardError):
	pass
	
def error_example2(a):
	n=int(a)
	if n==0 :
		raise DivisionError('invalid value: %s' % a)
	return 10/n
	
def error_example3(a):
	n=int(a)
	try:
		return 10/n
	except ZeroDivisionError,e:
		print 'ZeroDivisionError',e
		raise
	finally:
		print 'error_example3 finally'

if __name__=='__main__':
	print "error usage example"
	error_example(2)
	print '--------------------------------'
	error_example(0)
	print '--------------------------------'
	error_example('a')
	print '--------------------------------'
	error_example2(2)
	print '--------------------------------'
	try:
		error_example2(0)
	except StandardError:
		print 'error_example2 exception'
	print '--------------------------------'
	try:
		error_example3(0)
	except StandardError:
		print 'error_example3 exception'
	print '--------------------------------'

